// ======================== et 函数的实现（MD5） ========================
const et = (function() {
    // MD5 核心算法
    function md5(e, n) {
        function t(e, t) {
            return e << t | e >>> 32 - t
        }
        function r(e, t) {
            var n, r, i, o, a;
            return i = 2147483648 & e,
            o = 2147483648 & t,
            n = 1073741824 & e,
            r = 1073741824 & t,
            a = (1073741823 & e) + (1073741823 & t),
            n & r ? 2147483648 ^ a ^ i ^ o : n | r ? 1073741824 & a ? 3221225472 ^ a ^ i ^ o : 1073741824 ^ a ^ i ^ o : a ^ i ^ o
        }
        function i(e, i, o, a, s, l) {
            return e = r(e, r(r(function(e, t, n) {
                return e & t | ~e & n
            }(i, o, a), s)),
            r(t(e, l), i)
        }
        function o(e, i, o, a, s, l) {
            return e = r(e, r(r(function(e, t, n) {
                return e & n | t & ~n
            }(i, o, a), s)),
            r(t(e, l), i)
        }
        function a(e, i, o, a, s, l) {
            return e = r(e, r(r(function(e, t, n) {
                return e ^ t ^ n
            }(i, o, a), s)),
            r(t(e, l), i)
        }
        function s(e, i, o, a, s, l) {
            return e = r(e, r(r(function(e, t, n) {
                return t ^ (e | ~n)
            }(i, o, a), s)),
            r(t(e, l), i)
        }
        function l(e) {
            for (var t = "", n = 0; n < 32 * e.length; n += 8)
                t += String.fromCharCode(e[n >> 5] >>> n % 32 & 255);
            return t
        }
        function c(e) {
            var t = [];
            for (t[(e.length >> 2) - 1] = undefined,
            n = 0; n < t.length; n += 1)
                t[n] = 0;
            for (n = 0; n < 8 * e.length; n += 8)
                t[n >> 5] |= (255 & e.charCodeAt(n / 8)) << n % 32;
            return t
        }
        function u(e) {
            for (var t = "", n = 0; n < e.length; n += 1)
                t += String.fromCharCode(255 & e.charCodeAt(n));
            return t
        }
        function d(e) {
            for (var t, n = e.length, r = n + 8, i = 16 * ((r - r % 64) / 64 + 1), o = new Array(i - 1), a = 0, s = 0; s < n; )
                a = s % 4 * 8,
                o[t = (s - s % 4) / 4] = o[t] | e.charCodeAt(s) << a,
                s += 1;
            return a = s % 4 * 8,
            o[t = (s - s % 4) / 4] = o[t] | 128 << a,
            o[i - 2] = 8 * n,
            o[i - 1] = n >>> 29,
            o
        }
        function f(e) {
            for (var t = "", n = 0; n < 4 * e.length; n += 1)
                t += "0123456789abcdef".charAt(e[n >> 2] >> n % 4 * 8 + 4 & 15) + "0123456789abcdef".charAt(e[n >> 2] >> n % 4 * 8 & 15);
            return t
        }
        var h, p, g, m, y, v, b, w, x, C = d(e), S = [1732584193, -271733879, -1732584194, 271733878];
        for (h = 0; h < C.length; h += 16) {
            p = S[0],
            g = S[1],
            m = S[2],
            y = S[3],
            v = i(p, g, m, y, C[h + 0], 7, -680876936),
            y = i(y, v, p, g, C[h + 1], 12, -389564586),
            g = i(g, y, v, p, C[h + 2], 17, 606105819),
            p = i(p, g, y, v, C[h + 3], 22, -1044525330),
            v = i(p, g, y, v, C[h + 4], 7, -176418897),
            y = i(y, v, p, g, C[h + 5], 12, 1200080426),
            g = i(g, y, v, p, C[h + 6], 17, -1473231341),
            p = i(p, g, y, v, C[h + 7], 22, -45705983),
            v = i(p, g, y, v, C[h + 8], 7, 1770035416),
            y = i(y, v, p, g, C[h + 9], 12, -1958414417),
            g = i(g, y, v, p, C[h + 10], 17, -42063),
            p = i(p, g, y, v, C[h + 11], 22, -1990404162),
            v = i(p, g, y, v, C[h + 12], 7, 1804603682),
            y = i(y, v, p, g, C[h + 13], 12, -40341101),
            g = i(g, y, v, p, C[h + 14], 17, -1502002290),
            p = i(p, g, y, v, C[h + 15], 22, 1236535329),
            v = o(p, g, y, v, C[h + 1], 5, -165796510),
            y = o(y, v, p, g, C[h + 6], 9, -1069501632),
            g = o(g, y, v, p, C[h + 11], 14, 643717713),
            p = o(p, g, y, v, C[h + 0], 20, -373897302),
            v = o(p, g, y, v, C[h + 5], 5, -701558691),
            y = o(y, v, p, g, C[h + 10], 9, 38016083),
            g = o(g, y, v, p, C[h + 15], 14, -660478335),
            p = o(p, g, y, v, C[h + 4], 20, -405537848),
            v = o(p, g, y, v, C[h + 9], 5, 568446438),
            y = o(y, v, p, g, C[h + 14], 9, -1019803690),
            g = o(g, y, v, p, C[h + 3], 14, -187363961),
            p = o(p, g, y, v, C[h + 8], 20, 1163531501),
            v = o(p, g, y, v, C[h + 13], 5, -1444681467),
            y = o(y, v, p, g, C[h + 2], 9, -51403784),
            g = o(g, y, v, p, C[h + 7], 14, 1735328473),
            p = o(p, g, y, v, C[h + 12], 20, -1926607734),
            v = a(p, g, y, v, C[h + 5], 4, -378558),
            y = a(y, v, p, g, C[h + 8], 11, -2022574463),
            g = a(g, y, v, p, C[h + 11], 16, 1839030562),
            p = a(p, g, y, v, C[h + 14], 23, -35309556),
            v = a(p, g, y, v, C[h + 1], 4, -1530992060),
            y = a(y, v, p, g, C[h + 4], 11, 1272893353),
            g = a(g, y, v, p, C[h + 7], 16, -155497632),
            p = a(p, g, y, v, C[h + 10], 23, -1094730640),
            v = a(p, g, y, v, C[h + 13], 4, 681279174),
            y = a(y, v, p, g, C[h + 0], 11, -358537222),
            g = a(g, y, v, p, C[h + 3], 16, -722521979),
            p = a(p, g, y, v, C[h + 6], 23, 76029189),
            v = a(p, g, y, v, C[h + 9], 4, -640364487),
            y = a(y, v, p, g, C[h + 12], 11, -421815835),
            g = a(g, y, v, p, C[h + 15], 16, 530742520),
            p = a(p, g, y, v, C[h + 2], 23, -995338651),
            v = s(p, g, y, v, C[h + 0], 6, -198630844),
            y = s(y, v, p, g, C[h + 7], 10, 1126891415),
            g = s(g, y, v, p, C[h + 14], 15, -1416354905),
            p = s(p, g, y, v, C[h + 5], 21, -57434055),
            v = s(p, g, y, v, C[h + 12], 6, 1700485571),
            y = s(y, v, p, g, C[h + 3], 10, -1894986606),
            g = s(g, y, v, p, C[h + 10], 15, -1051523),
            p = s(p, g, y, v, C[h + 1], 21, -2054922799),
            v = s(p, g, y, v, C[h + 8], 6, 1873313359),
            y = s(y, v, p, g, C[h + 15], 10, -30611744),
            g = s(g, y, v, p, C[h + 6], 15, -1560198380),
            p = s(p, g, y, v, C[h + 13], 21, 1309151649),
            v = s(p, g, y, v, C[h + 4], 6, -145523070),
            y = s(y, v, p, g, C[h + 11], 10, -1120210379),
            g = s(g, y, v, p, C[h + 2], 15, 718787259),
            p = s(p, g, y, v, C[h + 9], 21, -343485551),
            S[0] = r(p, S[0]),
            S[1] = r(g, S[1]),
            S[2] = r(y, S[2]),
            S[3] = r(v, S[3])
        }
        return n && n.asBytes ? l(S) : n && n.asString ? u(l(S)) : f(S)
    }
    return md5;
})();

// ======================== en 函数的实现 ========================
function en(e, t) {
    var n = function(e) {
        e = e.replace(/\r\n/g, "\n");
        for (var t = "", n = 0; n < e.length; n++) {
            var r = e.charCodeAt(n);
            r < 128 ? t += String.fromCharCode(r) : (r > 127 && r < 2048 ? t += String.fromCharCode(r >> 6 | 192) : (t += String.fromCharCode(r >> 12 | 224),
            t += String.fromCharCode(r >> 6 & 63 | 128)),
            t += String.fromCharCode(63 & r | 128))
        }
        return t
    }
    , r = "A4NjFqYu5wPHsO0XTdDgMa2r1ZQocVte9UJBvk6/7=yRnhISGKblCWi+LpfE8xzm3"
    , i = function(e) {
        var t, i, o, a, s, l, c, u = "", d = 0;
        for (e = n(e); d < e.length; )
            t = e.charCodeAt(d++),
            i = e.charCodeAt(d++),
            o = e.charCodeAt(d++),
            a = t >> 2,
            s = (3 & t) << 4 | i >> 4,
            l = (15 & i) << 2 | o >> 6,
            c = 63 & o,
            isNaN(i) ? l = c = 64 : isNaN(o) && (c = 64),
            u = u + r.charAt(a) + r.charAt(s) + r.charAt(l) + r.charAt(c);
        return u
    }
    , o = "test"
    , a = new Date().getTime()
    , s = "undefined" == typeof window ? global : window;
    // 检测浏览器环境
    // if (s && s.navigator && s.navigator.userAgent && s.alert) {
    //     o = "test";
    // }
    // 拼接输入字符串
    var input = [
        a, 
        o, 
        e, 
        ("[object Object]" === Object.prototype.toString.call(t) || "[object Array]" === Object.prototype.toString.call(t)) 
            ? JSON.stringify(t) 
            : ""
    ].join("");
    // 计算 X-s 和 X-t
    return {
        "X-s": i(et(input)), // 先 MD5，再自定义 Base64
        "X-t": a // 直接返回时间戳
    };
}

// ======================== 测试用例 ========================
const e = "/api/leona/rtb/account/balance?vSellerId=5f7fbf79b793c500016a9a37";
const t = null; // 或传入对象/数组参数

const result = en(e, t);
console.log("生成的 X-s:", result["X-s"]);
console.log("生成的 X-t:", result["X-t"]);